package com.hnhy.hnhyscms_ws.controller;

import com.hnhy.hnhyscms_ws.entity.Cpoutsto;
import com.hnhy.hnhyscms_ws.entity.Cpstorage;
import com.hnhy.hnhyscms_ws.service.ICpoutstoService;
import com.hnhy.hnhyscms_ws.service.ICpstorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/cpoutsto")
public class CpoutstoController {
    @Autowired
    private ICpoutstoService iCpoutstoService;
    @Autowired
    private ICpstorageService iCpstorageService;
    @RequestMapping(value = "/checkSto",method= RequestMethod.POST)
    @ResponseBody
    public String checkSto(@RequestParam String productNo, @RequestParam Float amount){
        Cpstorage cpstorage=iCpstorageService.getByProductNo(productNo);
        if(cpstorage.getAmount()-amount<0)
            return "1";
        return "0";
    }
    @RequestMapping(value = "/addCpoutsto",method= RequestMethod.POST)
    @ResponseBody
    public synchronized void addCpoutsto(@RequestParam String productNo, @RequestParam Float amount, @RequestParam String orderNo){
        Cpstorage cpstorage=iCpstorageService.getByProductNo(productNo);
        Cpoutsto cpoutsto=new Cpoutsto();
        cpoutsto.setAmount(amount);
        cpoutsto.setBand(cpstorage.getBand());
        cpoutsto.setPrice(cpstorage.getPrice());
        cpoutsto.setProductName(cpstorage.getProductName());
        cpoutsto.setProductNo(productNo);
        cpoutsto.setSpec(cpstorage.getSpec());
        cpoutsto.setOrderNo(orderNo);
        cpoutsto.setUnit(cpstorage.getUnit());
        cpoutsto.setTotal(amount*cpstorage.getPrice());
        iCpoutstoService.addCpoutsto(cpoutsto);
        cpstorage.setAmount(cpstorage.getAmount()-amount);
        iCpstorageService.updateByProductNo(cpstorage);
    }
}
